package com.example.foodwebbackend.controller;

import com.example.foodwebbackend.dto.ApiResponse;
import com.example.foodwebbackend.entity.FoodCategory;
import com.example.foodwebbackend.service.FoodCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/categories")
@CrossOrigin(origins = "*")
public class FoodCategoryController {

    @Autowired
    private FoodCategoryService foodCategoryService;

    /**
     * 获取所有分类
     */
    @GetMapping
    public ApiResponse<List<FoodCategory>> getAllCategories() {
        try {
            List<FoodCategory> categories = foodCategoryService.getAllCategories();
            return ApiResponse.success("获取分类成功", categories);
        } catch (Exception e) {
            return ApiResponse.error("获取分类失败：" + e.getMessage());
        }
    }

    /**
     * 根据ID获取分类
     */
    @GetMapping("/{id}")
    public ApiResponse<FoodCategory> getCategoryById(@PathVariable Integer id) {
        try {
            FoodCategory category = foodCategoryService.getCategoryById(id);
            if (category == null) {
                return ApiResponse.error("分类不存在");
            }
            return ApiResponse.success("获取分类成功", category);
        } catch (Exception e) {
            return ApiResponse.error("获取分类失败：" + e.getMessage());
        }
    }

    /**
     * 添加分类（仅管理员）
     */
    @PostMapping
    public ApiResponse<FoodCategory> addCategory(@RequestBody FoodCategory category) {
        try {
            FoodCategory newCategory = foodCategoryService.addCategory(category);
            return ApiResponse.success("添加分类成功", newCategory);
        } catch (Exception e) {
            return ApiResponse.error("添加分类失败：" + e.getMessage());
        }
    }

    /**
     * 更新分类（仅管理员）
     */
    @PutMapping("/{id}")
    public ApiResponse<FoodCategory> updateCategory(@PathVariable Integer id, @RequestBody FoodCategory category) {
        try {
            category.setCategoryId(id);
            FoodCategory updatedCategory = foodCategoryService.updateCategory(category);
            return ApiResponse.success("更新分类成功", updatedCategory);
        } catch (Exception e) {
            return ApiResponse.error("更新分类失败：" + e.getMessage());
        }
    }

    /**
     * 删除分类（仅管理员）
     */
    @DeleteMapping("/{id}")
    public ApiResponse<String> deleteCategory(@PathVariable Integer id) {
        try {
            foodCategoryService.deleteCategory(id);
            return ApiResponse.success("删除分类成功", null);
        } catch (Exception e) {
            return ApiResponse.error("删除分类失败：" + e.getMessage());
        }
    }

    /**
     * 根据父级分类ID获取子分类
     */
    @GetMapping("/parent/{parentId}")
    public ApiResponse<List<FoodCategory>> getCategoriesByParentId(@PathVariable Integer parentId) {
        try {
            List<FoodCategory> categories = foodCategoryService.getCategoriesByParentId(parentId);
            return ApiResponse.success("获取子分类成功", categories);
        } catch (Exception e) {
            return ApiResponse.error("获取子分类失败：" + e.getMessage());
        }
    }
} 